﻿package com.bases {
	import com.bases.ScollBar.TBaseScrollBar;
	import com.bases.ScollBar.TControlScrollBar;
	import com.DragControl;
	import com.Interface.IItem;
	import com.Interface.IList;
	import com.greensock.TweenLite;
	import com.greensock.easing.Quart;
	import com.xjw.ArrayHelp;
	import config.TConfig;
	import flash.display.Sprite;
	import flash.events.TouchEvent;
	import flash.geom.Rectangle;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;

	/**
	 * 自已的List (增加手势托动)
	 * @author pethan
	 *
	 */
	public class MyList extends TUIComponent implements IList {
		public var length:int;
		//行距(默认为4)	(目前先为0不然有一个bug,就是多了会少显示列表:todo:)
		public var rowSpace:int = 0;
		//子项为类
		public var item:Class;
		//列数
		public var col:int = 1;
		//每行需要的列数
		public var needCol:int = 0;
		//行数
		public var row:int = 0;
		//所有项
		public var items:Array;
		//列距
		public var colSpace:int = 4;
		//滚动条
		private var scrollBar:TControlScrollBar;
		//添加到
		private var spriteContains:Sprite;
		//前后的差距为4,说明是点击按下的.
		private var moveDistance:Number = 5;
		private var lastY:Number;
		/**
		 * 没有设置行
		 */
		public var listRow:Number = 0;

		public function MyList(){
			super();
			items = new Array();
			spriteContains = new Sprite();
			this.addChild(spriteContains);
			scrollBar = new TControlScrollBar(spriteContains);
			this.addChild(scrollBar);
			scrollBar.visible = false;
		}

		override public function setSize(w:Number, h:Number):void {
			this.w = w;
			this.h = h;
			this.scrollBar.setPos(this.w - TBaseScrollBar.BUTTON_WIDTH - 1, 0);
			this.scrollBar.setSize(20, this.h);
			this.scrollRect = new Rectangle(0, 0, this.w, this.h);
			this.scrollBar.maskHeight = this.h;
			this.scrollBar.invalidateScrollBar();
		}

		/**
		 * 设置是否增加手势的识别
		 * @param value
		 *
		 */
		public function set touch(value:Boolean):void {
			if (value){
				Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
				TConfig.getInstance().mainStage.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
			} else {
				TConfig.getInstance().mainStage.removeEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
			}
		}

		public function onTouchBegin(event:TouchEvent):void {
			lastY = this.spriteContains.y;
			TweenLite.to(scrollBar, 0.5, {alpha: 1, ease: Quart.easeInOut});
			TConfig.getInstance().mainStage.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
			TConfig.getInstance().mainStage.addEventListener(TouchEvent.TOUCH_END, onTouchEnd);
			this.spriteContains.startTouchDrag(event.touchPointID, false);
		}

		public function onTouchMove(event:TouchEvent):void {
			spriteContains.x = 0;
			if (Math.abs(lastY - this.spriteContains.y) >= moveDistance){
				this.spriteContains.mouseChildren = false;
				this.spriteContains.mouseEnabled = false;
			}
			this.scrollBar.changeScrollV();
		}

		public function onTouchEnd(event:TouchEvent):void {
			this.stopTouchDrag(event.touchPointID);
			//如果没有滚动条,要拉回去
			this.spriteContains.mouseChildren = true;
			this.spriteContains.mouseEnabled = true;
			TConfig.getInstance().mainStage.removeEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
			TConfig.getInstance().mainStage.removeEventListener(TouchEvent.TOUCH_END, onTouchEnd);
			if (spriteContains.height > this.h && spriteContains.y < this.h - spriteContains.height){
				TweenLite.to(spriteContains, 1, {y: this.h - spriteContains.height - 20, ease: Quart.easeInOut});
			} else {
				TweenLite.to(spriteContains, 1, {y: 0, ease: Quart.easeInOut});
			}
			TweenLite.to(scrollBar, 1, {alpha: 0, ease: Quart.easeInOut});
		}

		public function addListChild(sprite:*):void {
			this.spriteContains.addChild(sprite);
			this.scrollBar.invalidateScrollBar();
		}

		public function removeListChild(sprite:Sprite):void {
			this.spriteContains.removeChild(sprite);
			this.scrollBar.invalidateScrollBar();
		}

		public function AddControl(can:TUIComponent):void {
			can.setPos(can.width * needCol, can.height * row);
			this.addListChild(can);
			if (listRow == 0){ /*说明是设置列,没有设置行*/
				needCol++;
				if (row >= 1){
					can.y += rowSpace * row;
				}
			} else { /*设置行,就以行为单位进行排列,目前先从第一行开始添加,以后会进行改正的*/
				row++;
				if (row == listRow){
					row = 0;
					needCol++;
				}
			}
			if (needCol >= 1){
				can.x += colSpace * (needCol - 1);
				if (needCol == col){
					needCol = 0;
					row++;
				}
			}
			length++;
		}

		//移除开之后重新计算行和和需要的列
		private function calRowAndCol():void {
			if (needCol != 0){
				needCol -= 1;
			} else {
				needCol = col - 1;
				row -= 1;
			}
			length--;
		}

		override protected function relayout():void {
			super.relayout();
			this.length = 0;
			this.needCol = 0;
			this.row = 0;
			for (var i:int = 0; i < this.items.length; i++){
				if (this.spriteContains.contains(this.items[i])){
					//有包含这项
					this.AddControl(this.items[i]);
				}
			}
		}

		//立即重新排列
		public function invalist():void {
			this.relayout();
		}

		/**
		 * 清除列表
		 *
		 */
		public function Clear():void {
			while (this.numChildren > 0){
				this.removeChildAt(0);
			}
			length = 0;
		}

		/**
		 *  todo:要改为index的位置
		 * @param value
		 * @param needRelayout
		 * @param isUnshift
		 *
		 */
		public function addItem(value:*, needRelayout:Boolean = true, isUnshift:Boolean = false):void {
			//			if(!(value as SCData).getEmpty()){
			//判断是否有存在,不存在的话就添加(为了视频方面的)
			for (var i:int = 0; i < this.items.length; i++){
				if ((this.items[i] as IItem).userData == value){
					return;
				}
			}
			var userListItem:* = new(item as Class)();
			IItem(userListItem).userData = value;
			this.AddControl(userListItem);
			if (isUnshift){
				items.unshift(userListItem);
			} else {
				items.push(userListItem);
			}
			if (needRelayout){
				this.relayout();
			}
			//			}
		}

		//移送项(增加项目的移送)
		public function removeItem(value:*, needRealyout:Boolean = true):void {
			for (var i:int = 0; i < this.items.length; i++){
				if ((this.items[i] as IItem).userData == value){
					if (this.spriteContains.contains(this.items[i])){
						if (i != this.items.length - 1 && !needRealyout){
							var lastItem:TUIComponent = findLastItem();
							if (lastItem != null && lastItem != this.items[i]){
								lastItem.setPos(this.items[i].x, this.items[i].y);
								calRowAndCol();
							}
						}
						this.removeListChild(this.items[i]);
						(this.items[i] as TUIComponent).dispose();
					} else {
						(this.items[i] as TUIComponent).parent.removeChild(this.items[i]);
							//DragControl.getInstance().stage.removeChild(this.items[i]);
					}
					ArrayHelp.deleteArray(this.items, i);
					break;
				}
			}
			if (needRealyout){
				this.relayout();
			}
		}

		//寻找最后的对象(找到X,Y最高的对象)
		private function findLastItem():TUIComponent {
			var lastItem:TUIComponent;
			if (this.items.length != 0){
				lastItem = this.items[0];
			}
			for (var i:int = 1; i < this.items.length; i++){
				if (lastItem.x <= this.items[i].x && lastItem.y <= this.items[i].y){
					lastItem = this.items[i];
				}
			}
			return lastItem;
		}

		/**
		 * 当前列表是否有包括此项
		 * @param value
		 * @return
		 *
		 */
		public function haveContainItem(value:*):Boolean {
			if (this.spriteContains.contains(value)){
				return true;
			}
			return false;
		}

		/**
		 * 移除所有子项
		 *
		 */
		public function removeAllItem():void {
			while (this.items.length > 0){
				this.removeListChild(this.items[0]);
				items.shift();
			}
			this.length = 0;
			this.needCol = 0;
			this.row = 0;
			items = new Array();
		}

		/**
		 * 检测这个列表项是否有在这个List里面
		 * @param value
		 * @return
		 *
		 */
		public function haveItem(value:*):Boolean {
			for (var i:int = 0; i < this.items.length; i++){
				if ((this.items[i] as IItem).userData == value){
					return true;
				}
			}
			return false;
		}

		private function checkValue(item:IItem, value:*):Boolean {
			if (item.userData == value){
				return true;
			}
			return false;
		}
	}
}